//
//  YWLittleVideoDetailController.m
//  videoTest3
//
//  Created by jun peng on 2022/6/10.
//

#import "YWLittleVideoDetailController.h"
#import "YWVideoView.h"
#import "YWVideoBrowserAnimator.h"


@interface YWLittleVideoDetailController ()<YWVideoViewDelegate,YWVideoBrowserAnimatorDismissDelegate>

@property (nonatomic, strong) YWVideoView * vedioView; // 播放器

@end

@implementation YWLittleVideoDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.vedioView = [YWVideoView videoView];
    
    self.vedioView.isNeedToolBar = YES;
    self.vedioView.isNeedLoadingView = YES;
    
    self.vedioView.delegate = self;
    
    self.vedioView.backgroundColor = [UIColor clearColor];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:self.vedioView];
    
    self.view.frame = CGRectMake(0, 0, Wi, He);
    
    self.navBar.alpha = 0;
    
//    self.vedioView.frame = self.view.bounds;
    self.vedioView.frame = CGRectMake(0, 0, Wi, He);
    
    
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    
    [self.view addGestureRecognizer:pan];
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
//    self.vedioView.frame = self.view.bounds;
        self.vedioView.frame = CGRectMake(0, 0, Wi, He);
    
    if (self.isAddAnotherViewToContinuePlay == YES) {
        //去其它view继续播放，不要先停止之前正在播放的、继续播放
        [self.vedioView continuePlayWithAnthorViewWithUrl:self.FileUrl WithMute:NO];
    }
    else{
        
    [self.vedioView startPlayWithUrl:self.FileUrl withMute:NO];
        
    }
    
    
}

-(void)videoViewDidTap:(YWVideoView *)videoView{
    
    [self dismiss];
   
}


-(void)dismiss{
    
//    [self.vedioView stopPlay];
    [self dismissViewControllerAnimated:YES completion:^{
        if ([self.delegate respondsToSelector:@selector(littleVideoDetailControllerDidDismissVC:)]) {
            [self.delegate littleVideoDetailControllerDidDismissVC:self];
        }
    }];
}


-(void)pan:(UIPanGestureRecognizer *)pan{
 
    CGPoint transP = [pan translationInView:self.view];
    
    switch (pan.state) {
        case UIGestureRecognizerStateBegan:
        {
//            self.saveBtn.hidden = YES;
//            self.closeBtn.hidden = YES;
            
            [self.vedioView pausePlay];
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            //事实证明、transform会改变frame值、所以这里是frame和transform共同配合完成、基本上是transform来做改值、frame来做条件判断
            
            
            //不能改变frame、因为内部控件没用autolayout，不会跟着变小、所以这里注释了
//            改变collectionView位置
//            self.collectionView.x = self.collectionView.x + transP.x;
//            self.collectionView.y = self.collectionView.y + transP.y;
//
////            改变self.view的backgroundColor的透明度
////            self.view.backgroundColor的alpha最大值是1 移动越远越接近0、1-比例
            CGFloat alpha = 1 - self.vedioView.y/He;
            self.view.backgroundColor = YWColorRGBA(0, 0, 0, alpha);
            
           //这里不要用绝对值、所以注释了
//            改变collectionView大小
//            比例正常是1、就是最大值是1
//            CGFloat ratio = 1 - fabs(self.collectionView.y)/He;
//            self.collectionView.width = self.view.width * ratio;
//            self.collectionView.height = self.view.height *ratio;
            
            
            self.vedioView.transform = CGAffineTransformTranslate(self.vedioView.transform, transP.x, transP.y);
            
            //因为缩放和移动的transform，所以只能用CGAffineTransformTranslate和CGAffineTransformScale
            
            //因为CGAffineTransformMakeTranslation是一次性改变的、所以注释了
//            self.collectionView.transform = CGAffineTransformMakeTranslation(self.collectionView.x + transP.x, self.collectionView.y + transP.y);
            
            //移动距离越大、除以固定He的分母后值就越大、再拿1减就值就越小（即scaleRatio越小）、所以移动距离越大、缩小得越厉害
            CGFloat scale_Ratio = 1- transP.y/He;
            
          

            YWLog(@"%f",scale_Ratio);
            
            //它这个transP是从开始移动到停止移动会变化一次、然后又清零、而x、y是累积变化
            YWLog(@"transP:%@,x:%f,y:%f",NSStringFromCGPoint(transP),self.vedioView.x,self.vedioView.y);
            
            //当collectionView.y大于等于0时，大小才发生变化
            if (self.vedioView.y>=0) {
                self.vedioView.transform = CGAffineTransformScale(self.vedioView.transform, scale_Ratio, scale_Ratio);
            }
            
            
           //不需要anchorPoint、默认就是0.5 0.5
//            self.collectionView.layer.anchorPoint = CGPointMake(0.5, 0.5);
          //因为CGAffineTransformMakeScale是一次性改变的、所以注释了
//            self.collectionView.transform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);
            
//            YWLog(@"%@",NSStringFromCGRect(self.collectionView.frame));
            
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            if (self.vedioView.y>He/3.0) {
                [self dismiss];
            }
            else{
                [UIView animateWithDuration:0.3 animations:^{
                    
                    self.vedioView.transform = CGAffineTransformIdentity;
                    
                    self.view.backgroundColor = YWColorRGBA(0, 0, 0, 1);
                    
                }completion:^(BOOL finished) {
                    
//                        self.saveBtn.hidden = NO;
//                        self.closeBtn.hidden = NO;
                    
                    
                    [self.vedioView continuePlay];
                }];
            }
            
           
        }
            break;
            
        default:
            break;
    }
    
//    YWLog(@"%@",NSStringFromCGPoint(transP));
    
    
    //复位.
    [pan setTranslation:CGPointZero inView:self.view];
    
}

#pragma mark - AnimatorDismissDelegate
//-(UIImageView*)imageViewForDimissView{
//
//    // 1.创建UIImageView对象
//    UIImageView *imageView = [[UIImageView alloc] init];
//
//    //截图
//    [self.vedioView snapshot:^(UIImage * _Nonnull image) {
//        imageView.image = image;
//    }];
//
//    //转化为相对于window的frame
//    CGRect imageRectForWindow = [self.view convertRect:self.vedioView.frame toView:[UIApplication sharedApplication].keyWindow];
//
//    imageView.frame = imageRectForWindow;
//
//    // 3.设置imageView的属性
//    imageView.contentMode = UIViewContentModeScaleAspectFit;
////    imageView.clipsToBounds = YES;
//
//    return imageView;
//
//}

//因为SDK的视频截图会有闪烁效果不好、所以这里用获取播放器view一边播放一边缩小动画效果
-(UIView *)vedioViewForDimissView{
    
    YWVideoView *vedioView = [YWVideoView videoView];
    
    vedioView.backgroundColor = [UIColor clearColor];
    
    [vedioView continuePlayWithAnthorViewWithUrl:self.vedioView.urlString WithMute:YES];
    
    
    //转化为相对于window的frame
    CGRect imageRectForWindow = [self.view convertRect:self.vedioView.frame toView:[UIApplication sharedApplication].keyWindow];
    
    vedioView.frame = imageRectForWindow;
    
    return vedioView;
    
}


@end
